import CustomShaderUpdate from "./CustomShaderUpdate"


export default class CustomRenderTexture {
    public texture: cc.RenderTexture = null
    public node: cc.Node = null
    public material: cc.Material = null
    public render: cc.RenderComponent = null
    public renderUpdate: CustomShaderUpdate = null
    constructor() {
        this.texture = new cc.RenderTexture()
    }

    initWithSize(width?: number, height?: number, depthStencilFormat?: number): void {
        this.texture.initWithSize(width, height, depthStencilFormat)
    }

    createNode(): cc.Node {
        this.node = new cc.Node()

        let sprite = this.node.addComponent(cc.Sprite)
        sprite.spriteFrame = new cc.SpriteFrame(this.texture)
        this.render = sprite

        this.renderUpdate = this.node.addComponent(CustomShaderUpdate)
        return this.node
    }

    setMaterial(mat: cc.Material): void {
        this.material = cc.Material.getInstantiatedMaterial(mat, this.render)
        this.render.setMaterial(0, this.material)
        this.renderUpdate.component = this.render
        //this.material.setProperty('texture', this.texture, true)
    }

}